<template>
  <div id="detail-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName">
          <el-tab-pane label="登录账户设置" name="first">
            <!-- 放置表单 -->
            <el-form ref="userForm" label-width="120px" style="margin-left: 120px; margin-top:30px" :model="userForm" :rules="rules">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userForm.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password">
                <el-input v-model="userForm.password" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="updateUserInfo">更新</el-button>
                <el-button type="primary" @click="$router.back()">返回</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>

          <el-tab-pane label="个人详情" name="second" lazy>
            <user-detail />
          </el-tab-pane>

          <el-tab-pane label="岗位信息" name="third">
            <job-detail />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserInfoById, updateUserInfo } from '@/api/user.js'
import UserDetail from './components/user-detail.vue'
import JobDetail from './components/job-detail.vue'
export default {
  name: 'Detail',
  components: {
    UserDetail,
    JobDetail
  },
  data() {
    return {
      activeName: 'first',
      userForm: {},
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.loadUserInfo()
  },
  methods: {
    // 获取用户信息
    async loadUserInfo() {
      const res = await getUserInfoById(this.$route.params.id)
      // console.log(res)
      this.userForm = res
    },
    // 更新用户信息
    async updateUserInfo() {
      try {
        await this.$refs.userForm.validate()
        // 校验成功
        await updateUserInfo(this.userForm)
        this.$message.success('更新成功')
      } catch (err) {
        this.$message.error('更新失败')
      }
    }
  }
}
</script>

<style lang='less' scoped>
</style>
